package dyyx;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibTest  {
	
	public static void main(String[] args) throws Exception {  
		Hello hello = new Hello();
		Hello proxy = createProxy(hello);
        System.out.println(proxy.sayHello("dyyx"));  

		 
    }  
	
	
	  private static Hello createProxy(final Hello delegate) throws Exception {  
	        Enhancer enhancer = new Enhancer();  
	        enhancer.setCallback(new CglibInterceptor(delegate));  
	        // enhancer.setInterfaces(new Class[] { CountService.class });  
	        enhancer.setSuperclass(Hello.class); 
	        Hello proxy = (Hello) enhancer.create();  
	        return proxy;  
	    }  
	  
	    private static class CglibInterceptor implements MethodInterceptor {  
	          
	        final Object delegate;  
	  
	        CglibInterceptor(Object delegate) {  
	            this.delegate = delegate;  
	        }  
	  
	        public Object intercept(Object object, Method method, Object[] objects,  
	                MethodProxy methodProxy) throws Throwable { 
	        	System.out.println(method.getName()+" start "+LocalDateTime.now());
	            return methodProxy.invoke(delegate, objects);  
	        }  
	    }  
  
 
}
	